<?php
App::uses('AppController', 'Controller');
App::uses('AppUtility','Lib');
/**
 * Transactions Controller
 *
 */
class TransactionsController extends AppController {
	public $uses = array('User','Transaction');
	public function beforeFilter() {
			parent::beforeFilter();
			$this->layout = 'admin';
	}
	/**
	 * 
	 */
	public function index(){
		$allUser = $this->User->find('all');
		if ($this->request->query) {
			$Username = base64_decode($this->request->query['U']) ;
			$Type	  = $this->request->query['Type'];
			if($this->request->query['U'] != 1 ){
					if($Type == 0){
						$arrCondition = array(
									'name_advice'	    =>$Username
								);
					}elseif ($Type == 1){
						$arrCondition = array(
									'name_need_advice'	=>$Username
						);
					}else{
						$arrCondition = array('OR'=>array(
										'name_need_advice' =>$Username,
										'name_advice'	   =>$Username
								));
					}
					$this->paginate = array(
							'conditions'	=> $arrCondition,
							'fields'        => '*',
							'limit'         => 4
					);
					$arrResult  = $this->paginate('Transaction');
					//get total money 
					$totaMoney = $this->TotalMoney($arrCondition);
					
			}else{
				$this->paginate = array(
						'fields'        => '*',
						'limit'         => 4
				);
				$arrResult = $this->paginate('Transaction');
				$totaMoney = $this->TotalMoney("*");
			}
		}else{
			$this->paginate = array(
					'fields'        => '*',
					'limit'         => 4
			);
			$arrResult = $this->paginate('Transaction');
			$totaMoney = $this->TotalMoney("*");
		}
		
		
		$startDate =date('Y-m-d', time() - 86400) ;
		$endDate   =date('Y-m-d', time()) ;
		$this->set('TotalMoney',$totaMoney);
		$this->set('Dataview',$this->zendHTML($arrResult));
		$this->set('allUser',$allUser) ;
		$this->set('StartDate',$startDate) ;
		$this->set('EndDate',$endDate) ;
	}
	/**
	 * Function ZendHTML
	 * @author Minh Phan
	 * @param unknown $arrUser
	 * @return string
	 */
	public function zendHTML($arrTran){
		if($arrTran){
			foreach ($arrTran as $tranInfo){
			 $totalTime = $tranInfo['Transaction']['start_time'] - $tranInfo['Transaction']['end_time'];
				$dataView[]=	"<tbody>
										<tr class='odd'>
											<td>". $tranInfo['Transaction']['id']."</td>
											<td >". $tranInfo['Transaction']['name_need_advice']."</td>
											<td class ='username'>". $tranInfo['Transaction']['skype_need_advice']."</td>
											<td>".$tranInfo['Transaction']['name_advice']."</td>
											<td>".$tranInfo['Transaction']['skype_advice']."</td>
											<td>".date('Y:m:d - H:i',$tranInfo['Transaction']['start_time'])."</td>
											<td>".date('Y:m:d - H:i',$tranInfo['Transaction']['end_time'])."</td>
											<td>".$totalTime."</td>
											<td>".$tranInfo['Transaction']['money_in']."</td>
											<td>".$tranInfo['Transaction']['money_out']."</td>
										</tr>
	
									</tbody>
									";
			}
			return $dataView;
		}
	}
	/**
	 * Function TotalMoney
	 * @author MinhPhan
	 * @param unknown $arrConditions
	 */
	public function TotalMoney($arrConditions){
		$totalTime = 0;
		$totalMoneyIn = 0;
		$totalMoneyOut = 0;
		if($arrConditions != '*'){
			$arrData = $this->Transaction->find('all',array('conditions' => $arrConditions));
		}else{
			$arrData = $this->Transaction->find('all');
		}
		foreach ($arrData as $item){
			$totalMoneyIn = $totalMoneyIn + $item['Transaction']['money_in'];
			$totalMoneyOut = $totalMoneyOut + $item['Transaction']['money_out'];
		}
		$arrResult =array(
					'money_in'  =>$totalMoneyIn,
					'money_out' =>$totalMoneyOut
				);
		
		return $arrResult;
	}

}
